package cn.lik.airTycoon.http;

import java.util.Map;

import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.apache.log4j.Logger;

import cn.lik.airTycoon.http.bean.AirTycoonBean;

public class AirTycoonHttp extends LikHttp {
	protected AirTycoonHttp() {
		super();
	}

	public static final String SESSION = "X-Anansi-Session";
	public static final String CHECKSUM = "X-Game-Checksum";

	protected AirTycoonHttp(String url) throws Exception {
		super(url);
		setMethod(LikHttp.PUT);
		addHeader(LikHttp.USER_AGENT, "Anansi/1.0 Paros/3.2.13");
		addHeader("X-Anansi-Session", "28f176a1-2e13-4bea-bce6-78883fe6af9d");
		addHeader("X-Game-Checksum", "e11fd91c50066045f4be659d42c4aece, value");
	}

	protected AirTycoonHttp(String url, String session, String checksum, String datas) throws Exception {
		super(url);
		setMethod(LikHttp.PUT);
		addHeader(LikHttp.USER_AGENT, "Anansi/1.0 Paros/3.2.13");
		addHeader("X-Anansi-Session", "28f176a1-2e13-4bea-bce6-78883fe6af9d");
		addHeader("X-Game-Checksum", "e11fd91c50066045f4be659d42c4aece, value");
		addHeader(AirTycoonHttp.SESSION, session);
		addHeader(AirTycoonHttp.CHECKSUM, checksum);
		addDatas(datas);
	}

	public static AirTycoonHttp iniAirTycoonHttp(String url) {
		try {
			AirTycoonHttp likHttp = new AirTycoonHttp(url);
			return likHttp;
		} catch (Exception e) {
			Logger.getLogger(AirTycoonHttp.class).error("init error:", e);
			return null;
		}
	}

	public AirTycoonBean result() {
		AirTycoonBean bean = new AirTycoonBean();
		JSONObject obj = JSONObject.fromObject(connectToResult());
		obj = JSONObject.fromObject(obj.get("data"));
		bean.setBucks(Integer.parseInt(obj.get("bucks").toString()));
		bean.setLevel(Integer.parseInt(obj.get("level").toString()));
		return bean;
	}
}
